<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="提现店铺id" prop="withdSuppId">
        <el-input
          v-model="queryParams.withdSuppId"
          placeholder="请输入提现店铺id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现人appId" prop="withdAppid">
        <el-input
          v-model="queryParams.withdAppid"
          placeholder="请输入提现人appId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现人openId" prop="withdOpenid">
        <el-input
          v-model="queryParams.withdOpenid"
          placeholder="请输入提现人openId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现人姓名" prop="withdUserName">
        <el-input
          v-model="queryParams.withdUserName"
          placeholder="请输入提现人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现状态" prop="withdState">
        <el-input
          v-model="queryParams.withdState"
          placeholder="请输入提现状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现审批时间" prop="withdSpTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.withdSpTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择提现审批时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="提现打款时间" prop="withdDkTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.withdDkTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择提现打款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleAutoApproval('PROFIT_EXAMINE')"
        >自动打款审批</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleManualPayment"
        >手动打款</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleSetRemark"
          :disabled="multiple"
        >备注</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['suppliers:withdrawal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['suppliers:withdrawal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['suppliers:withdrawal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['suppliers:withdrawal:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" border stripe :data="withdrawalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" :selectable="handleSelectable" width="55" align="center" />
      <el-table-column label="提现店铺id" align="center" prop="withdSuppId" />
      <el-table-column label="提现人appId" align="center" prop="withdAppid" />
      <el-table-column label="提现人openId" align="center" prop="withdOpenid" />
      <el-table-column label="提现人姓名" align="center" prop="withdUserName" />
      <el-table-column label="提现人金额" align="center" prop="withdAmount" />
      <el-table-column label="提现状态" align="center" prop="withdState" :formatter="withdStateFormat"/>
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现审批时间" align="center" prop="withdSpTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.withdSpTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现打款时间" align="center" prop="withdDkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.withdDkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统打款状态" align="center" prop="withdDkState" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShowOrderList(scope.row)"
          >关联订单</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['suppliers:withdrawal:edit']"
          >修改</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['suppliers:withdrawal:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 订单列表 -->
    <el-dialog title="关联订单" :visible.sync="isShowOrderList" width="1500px" append-to-body>
      <el-table border stripe :data="orderList">
        <el-table-column label="订单号" align="center" prop="orderSn" />
        <el-table-column label="订单状态" align="center" prop="orderStatus" :formatter="orderStatusFormat"/>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品属性" align="center" prop="goodsAttr" />
        <el-table-column label="商品数量" align="center" prop="goodsNumber" />
        <el-table-column label="订单总价" align="center" prop="goodsAmount" />
        <el-table-column label="店长减免" align="center" prop="adminDiscount" />
        <el-table-column label="结算价" align="center" prop="cost" />
        <el-table-column label="收益" align="center" prop="commission" />
      </el-table>
    </el-dialog>
    <el-dialog title="手动打款" :visible.sync="isShowAmountInput" width="200" append-to-body>
      <el-input v-model="amountInputVal" type="digit" placeholder="请输入打款金额"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEditData('0')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改备注" :visible.sync="isShowRemarkInput" width="200" append-to-body>
      <el-input v-model="remarkInputVal" type="textarea" placeholder="请输入备注信息"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEditData('1')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改订单收益提现对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="唯一ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入唯一ID" />
        </el-form-item>
        <el-form-item label="是否有效" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入是否有效" />
        </el-form-item>
        <el-form-item label="租户ID" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户ID" />
        </el-form-item>
        <el-form-item label="提现店铺id" prop="withdSuppId">
          <el-input v-model="form.withdSuppId" placeholder="请输入提现店铺id" />
        </el-form-item>
        <el-form-item label="提现人appId" prop="withdAppid">
          <el-input v-model="form.withdAppid" placeholder="请输入提现人appId" />
        </el-form-item>
        <el-form-item label="提现人openId" prop="withdOpenid">
          <el-input v-model="form.withdOpenid" placeholder="请输入提现人openId" />
        </el-form-item>
        <el-form-item label="提现人姓名" prop="withdUserName">
          <el-input v-model="form.withdUserName" placeholder="请输入提现人姓名" />
        </el-form-item>
        <el-form-item label="提现人金额" prop="withdAmount">
          <el-input v-model="form.withdAmount" placeholder="请输入提现人金额" />
        </el-form-item>
        <el-form-item label="提现状态" prop="withdState">
          <el-input v-model="form.withdState" placeholder="请输入提现状态" />
        </el-form-item>
        <el-form-item label="提现审批时间" prop="withdSpTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.withdSpTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择提现审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提现打款时间" prop="withdDkTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.withdDkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择提现打款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统打款状态" prop="withdDkState">
          <el-input v-model="form.withdDkState" placeholder="请输入系统打款状态" />
        </el-form-item>
        <el-form-item label="系统打款信息" prop="withdDkRemark">
          <el-input v-model="form.withdDkRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listWithdrawal, getWithdrawal, delWithdrawal, addWithdrawal, updateWithdrawal, exportWithdrawal, getOrderByWithdid, plUpdateState } from "@/api/suppliers/withdrawal";

export default {
  name: "Withdrawal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单收益提现表格数据
      withdrawalList: [],
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isShowOrderList:false,
      isShowAmountInput:false,
      amountInputVal:"",
      isShowRemarkInput:false,
      remarkInputVal:"",
      withdStateOptions:[],
      orderStatusOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdSuppId: undefined,
        withdAppid: undefined,
        withdOpenid: undefined,
        withdUserName: undefined,
        withdState: undefined,
        withdSpTime: undefined,
        withdDkTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("SHOP_PROFIT_STATUS").then(response => {
        this.withdStateOptions = response.data;
    });
    this.getDicts("SHOP_ORDER_STATUS").then(response => {
        this.orderStatusOptions = response.data;
    });
  },
  methods: {
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    withdStateFormat(row, column) {
      return this.selectDictLabel(this.withdStateOptions, row.withdState);
    },
    /** 查询订单收益提现列表 */
    getList() {
      this.loading = true;
      listWithdrawal(this.queryParams).then(response => {
        this.withdrawalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        withdSuppId: undefined,
        withdAppid: undefined,
        withdOpenid: undefined,
        withdUserName: undefined,
        withdAmount: undefined,
        withdState: undefined,
        withdSpTime: undefined,
        withdDkTime: undefined,
        withdDkState: undefined,
        withdDkRemark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectable(row, index){
      if(row.withdState=="PROFIT_SUCCESS" || row.withdState=="PROFIT_PROCESSING"){
        return false
      }else{
        return true
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单收益提现";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithdrawal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单收益提现";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateWithdrawal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addWithdrawal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单收益提现编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWithdrawal(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单收益提现数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWithdrawal(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleAutoApproval(state){
      if(!this.ids || !this.ids.length){
        this.msgError("请选择需审批数据")
        return
      }
      this.$confirm('是否审核通过？"', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let idsStr = this.ids.join(",")
        plUpdateState({withdState:state, ids:idsStr}).then(res=>{
          if (response.code === 200) {
            this.msgSuccess("操作成功");
            this.getList();
          }else{
            this.msgError(response.msg);
          }
        })
      }).catch(() => {});
    },
    handleSetRemark(){
      let findObj = this.withdrawalList.find(obj=>{
          return obj.id == this.ids[0]
        })
      this.isShowRemarkInput = true
      this.remarkInputVal = findObj.withdDkRemark
    },
    handleManualPayment(){
      if(!this.ids || !this.ids.length){
        this.msgError("请选择需审批打款数据")
        return
      }
      if(this.ids.length==1){
        let findObj = this.withdrawalList.find(obj=>{
          return obj.id == this.ids[0]
        })
        this.isShowAmountInput = true
        this.amountInputVal = findObj.withdAmount
      }else{
        handleAutoApproval("PROFIT_SUCCESS")
      }
    },
    handleShowOrderList(row){
      this.isShowOrderList = true
      getOrderByWithdid(row.id).then(response => {
        if (response.code === 200) {
          this.orderList = response.data
        }
      });
    },
    handleEditData(flag){
      let params = {
        id:this.ids[0]
      }
      if(flag=='0'){
        params.withdAmount=this.amountInputVal
        params.withdState="PROFIT_SUCCESS"
      }
      if(flag=='1'){
        params.withdDkRemark=this.remarkInputVal
      }
      updateWithdrawal(params).then(response => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.isShowAmountInput = false
          this.isShowRemarkInput = false
          this.amountInputVal = ""
          this.remarkInputVal = ""
          this.getList();
        }else{
          this.msgError(response.msg)
        }
      });
    }
  }
};
</script>
